#include "StdAfx.h"
#include "Utility.h"

bool Utility::isDataLoaded=false;

Utility::Utility(void)
{}

Utility::~Utility(void)
{}



bool Utility::InitDataFromXML( string fileName, vector<NodePoint>& GlobalNodesTable )
{

	XMLPlatformUtils::Initialize();
	XercesDOMParser* parser= new XercesDOMParser();
	parser->parse(fileName.c_str());

	DOMNode* node=parser->getDocument();
	DOMDocument* document=dynamic_cast<DOMDocument*>(node);
	if (document!=NULL)
	{
		//getdocumentElement return the root element
		if(!ParseWithXerces(document->getDocumentElement(), GlobalNodesTable))
		{
			MessageBox(NULL, L"Invalid Data File!", L"", MB_OK);
			delete parser;
			XMLPlatformUtils::Terminate();
			return false;
		}
	}

	delete parser;
	XMLPlatformUtils::Terminate();

	return true;
}

bool Utility::ParseWithXerces( const DOMNode* inNode, vector<NodePoint>& table )
{
	const DOMElement& elementNode=dynamic_cast<const DOMElement&>(*inNode);
	char* tName=XMLString::transcode(elementNode.getTagName());
	if (std::string(tName)!="NodesInfo")//valid root node, as valid file
	{
		XMLString::release(&tName);
		return false;
	}
	XMLString::release(&tName);

	XMLCh* tagNodesInfo=XMLString::transcode("NodesInfo");
	XMLCh* tagNode=XMLString::transcode("Node");
	XMLCh* tagNeighbour=XMLString::transcode("Neighbour");
	XMLCh* tagNodesNumbers=XMLString::transcode("NodesNumbers");
	XMLCh* tagXpos=XMLString::transcode("Xpos");
	XMLCh* tagYpos=XMLString::transcode("Ypos");
	XMLCh* tagName=XMLString::transcode("Name");
	XMLCh* tagDis=XMLString::transcode("Dis");

	char* nodesNumbers=XMLString::transcode(elementNode.getAttribute(tagNodesNumbers));
	int numbers=atoi(nodesNumbers);
	for (int i=0; i<numbers; i++)
	{
		NodePoint node;
		node.setNodePointRadius(RADIUS);
		node.setNodeNum(i+1);
		table.push_back(node);
	}
	XMLString::release(&nodesNumbers);
	
	int maxX=0, maxY=0;
	DOMNodeList *pNodeList=elementNode.getElementsByTagName(tagNode);
	for (int i=0; i<pNodeList->getLength(); ++i)
	{
		DOMElement* theEle=dynamic_cast<DOMElement*>(pNodeList->item(i));
		char* nodeNum=XMLString::transcode(theEle->getAttribute(tagName));
		NodePoint& theNode=table[atoi(nodeNum)-1];
		char* xPos=XMLString::transcode(theEle->getAttribute(tagXpos));
		char* yPos=XMLString::transcode(theEle->getAttribute(tagYpos));
		int x=atoi(xPos);
		int y=atoi(yPos);
		theNode.setNodeCoarsePos(x, y);
		if (x>maxX)
			maxX=x;
		if (y>maxY)
			maxY=y;
		XMLString::release(&xPos);
		XMLString::release(&yPos);
		XMLString::release(&nodeNum);
		
		DOMNodeList *pChildList=theEle->getElementsByTagName(tagNeighbour);
		for (int j=0; j<pChildList->getLength(); ++j)
		{
			NeighbourInfo theNeighbour;
			DOMElement* theNeighEle=dynamic_cast<DOMElement*>(pChildList->item(j));
			char* neighNum=XMLString::transcode(theNeighEle->getAttribute(tagName));
			theNeighbour.neighNum=atoi(neighNum);
			char* neighDis=XMLString::transcode(theNeighEle->getAttribute(tagDis));
			theNeighbour.distance=atof(neighDis);
			//all the pheromone start with 1
			theNeighbour.pheromone=1.0;
			XMLString::release(&neighDis);
			XMLString::release(&neighNum);
			theNode.mNeighbourInfoArray.push_back(theNeighbour);
		}
	}
	NodePoint::setDrawInterval(maxX, maxY);
	for (size_t i=0; i<table.size(); ++i)
	{
		table[i].setNodeRealCenterPos();
	}
	XMLString::release(&tagName);
	XMLString::release(&tagNodesInfo);
	XMLString::release(&tagNode);
	XMLString::release(&tagNeighbour);
	XMLString::release(&tagXpos);
	XMLString::release(&tagYpos);
	XMLString::release(&tagDis);
	XMLString::release(&tagNodesNumbers);

	return true;
}


std::string Utility::ConvertNumberToString( float nodeNum )
{
	ostringstream outString;
	outString<<nodeNum;
	return outString.str();
}
